package com.quickcancellation.crm.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quickcancellation.crm.config.LogAspect;
import com.quickcancellation.crm.pojo.dto.PageDto;
import com.quickcancellation.crm.pojo.dto.PageParamsDto;
import com.quickcancellation.crm.pojo.dto.ResponseDto;
import com.quickcancellation.crm.pojo.entity.Distributor;
import com.quickcancellation.crm.pojo.entity.Salearea;
import com.quickcancellation.crm.pojo.entity.Userinfo;
import com.quickcancellation.crm.pojo.vo.DicKeyValVo;
import com.quickcancellation.crm.pojo.vo.PrincipalVo;
import com.quickcancellation.crm.service.IDictionaryService;
import com.quickcancellation.crm.service.IDistributorService;
import com.quickcancellation.crm.service.ISaleareaService;
import com.quickcancellation.crm.service.IUserinfoService;
import com.quickcancellation.crm.utils.DateUtils;
import com.quickcancellation.crm.utils.PageUtils;
import com.quickcancellation.crm.utils.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.MapUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-02-28
 */

@RestController
@RequestMapping("/distributor")
@CrossOrigin
@Api(tags = "经销商管理")
@SaCheckLogin
@SaCheckRole("distributor")
public class DistributorController {

    @Resource
    private IDistributorService distributorService;

    @Resource
    private IUserinfoService userinfoService;

    @Resource
    private IDictionaryService dictionaryService;

    @Resource
    private ISaleareaService saleareaService;

    @Resource
    HttpServletRequest request;

    @PostMapping("/list")
    @ApiOperation("分页查询列表")
    @LogAspect
    public ResponseDto getdistributorList(@RequestBody PageParamsDto<Map> params) {

        PageDto<Map> pageDto = PageUtils.getPageParams(params);

        List<Map> distributorList = distributorService.getdistributorList(pageDto);

        return ResponseUtil.success(distributorList.size(), distributorList);
    }

    @PostMapping("/save")
    @ApiOperation("保存经销商")
    @LogAspect
    public ResponseDto savedistributor(@RequestBody Distributor distributor) {
        if (distributor.getDistributorName() == null) {
            return ResponseUtil.fail("经销商名称为空");
        }

        if (distributor.getDistributorCode() == null) {
            return ResponseUtil.fail("经销商编码为空");
        }

        if (distributorService.list(new QueryWrapper<Distributor>().eq("distributorName", distributor.getDistributorName())).size() > 0) {
            return ResponseUtil.fail("经销商名称已存在");
        }

        if (distributorService.list(new QueryWrapper<Distributor>().eq("distributorCode", distributor.getDistributorCode())).size() > 0) {
            return ResponseUtil.fail("经销商编码已存在");
        }

        Userinfo userInfo = userinfoService.getUserInfoByRequest(request);
        String usernumber = userInfo.getUsernumber();

        distributor.setCreateOp(usernumber);
        distributor.setUpdateOp(usernumber);
        distributor.setUpdateTime(DateUtils.getNow());
        distributor.setCreateTime(DateUtils.getNow());

        return ResponseUtil.ok(distributorService.save(distributor));

    }

    @PostMapping("/delete")
    @ApiOperation("删除经销商")
    @LogAspect
    public ResponseDto savedistributor(@RequestBody Map params) {
        String distributorId = MapUtils.getString(params, "id");

        boolean result = distributorService.removeById(distributorId);

        return ResponseUtil.ok(result);

    }

    @PostMapping("/update")
    @ApiOperation("修改经销商")
    @LogAspect
    public ResponseDto updatedistributor(@RequestBody Distributor distributor) {

        if (distributor.getId() == null){
            ResponseUtil.fail("[id]为空");
        }
        distributor.setDistributorCode(null);
        distributor.setDistributorName(null);

        distributor.setCreateTime(null);
        distributor.setCreateOp(null);

        Userinfo userInfo = userinfoService.getUserInfoByRequest(request);

        String usernumber = userInfo.getUsernumber();

        distributor.setUpdateOp(usernumber);
        distributor.setUpdateTime(DateUtils.getNow());

        boolean result = distributorService.updateById(distributor);

        return ResponseUtil.ok(result);

    }

    @PostMapping("/getdistributorType")
    @ApiOperation("获取经销商类型")
    @LogAspect
    public ResponseDto getdistributorTypeList() {

        List<DicKeyValVo> dicList = dictionaryService.getDicKeyAndValByModel("经销商类型");

        return ResponseUtil.success(dicList.size(),dicList);

    }


    @PostMapping("/getDivideType")
    @ApiOperation("获取经销商渠道")
    @LogAspect
    public ResponseDto getDivideTypeList() {

        List<DicKeyValVo> dicList = dictionaryService.getDicKeyAndValByModel("经销商渠道");

        return ResponseUtil.success(dicList.size(),dicList);

    }

    @PostMapping("/getprincipalList")
    @ApiOperation("获取客户负责人")
    @LogAspect
    public ResponseDto getdistributorPrincipalList() {

        List<PrincipalVo> principalVoList = userinfoService.getPrincipalListByRole("经销商负责人");

        return ResponseUtil.success(principalVoList.size(),principalVoList);

    }


    @PostMapping("/getSaleAreaList")
    @ApiOperation("获取营销区域列表")
    @LogAspect
    public ResponseDto getSaleAreaList() {

//        List<PrincipalVo> principalVoList = userinfoService.getdistributorPrincipalListByRole("经销商负责人");

        QueryWrapper<Salearea> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","saleName");
        List<Salearea> saleAreaList = saleareaService.list(queryWrapper);
        return ResponseUtil.success(saleAreaList.size(),saleAreaList);

    }
}
